<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$chars = Characters::model()->findAll();

		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index', array('chars'=>$chars));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */

	// Translit downloaded file
	function read_file_docx($filename){

		    $striped_content = ''; 
		    $content = ''; 

		    if(!$filename || !file_exists($filename)) return false;  

		    $zip = zip_open($filename);

		    if (!$zip || is_numeric($zip)) return false; 

		    while ($zip_entry = zip_read($zip)) { 

		        if (zip_entry_open($zip, $zip_entry) == FALSE) continue; 

		        if (zip_entry_name($zip_entry) != "word/document.xml") continue; 

		        $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); 

		        zip_entry_close($zip_entry); 
		    }// end while 

		    zip_close($zip); 

		    //echo $content; 

		    //file_put_contents('1.xml', $content);  

		    $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); 
		    $chars = Characters::model()->findAll();
		    foreach ($chars as $c){
		    	$content = mb_ereg_replace($c->cyril, $c->qazaqparat, $content);
		    }
			
		    $content = str_replace('</w:r></w:p>', "\r\n", $content); 
		    $striped_content = strip_tags($content); 

		 //   return $striped_content; 
		require('tfpdf.php');

		$pdf = new tFPDF();
		$pdf->AddPage();

		// Add a Unicode font (uses UTF-8)
		$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
		$pdf->SetFont('DejaVu','',14);

		// Load a UTF-8 string from a file and print it//
		$txt = $striped_content;
		$pdf->Write(8,$txt);

		// Select a standard font (uses windows-1252)
		$pdf->SetFont('Arial','',14);
		$pdf->Ln(100);


		//$pdf->Output();
		$pdf->Output('translit.pdf','D'); 
}
	
	public function actionFileTranslit(){

		$filename = $_FILES["file"]["name"];// or /var/www/html/file.docx

		$content = $this->read_file_docx($filename); 
		if($content !== false) { 

		    echo nl2br($content); 
		}  
		else { 
		    echo 'Couldn\'t the file. Please check that file.'; 
		} 
	}




}